package com.only4play.httpapt.common.http.apache;

import com.only4play.httpapt.common.http.RequestHttp;
import com.only4play.httpapt.common.http.SimpleGetRequestExecutor;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.net.URIAuthority;

import java.io.IOException;

/**
 * @author liyuncong
 * @version 1.0
 * @file ApacheSimpleGetRequestExecutor
 * @brief ApacheSimpleGetRequestExecutor
 * @details ApacheSimpleGetRequestExecutor
 * @date 2024-08-09
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-08-09               liyuncong          Created
 */
public class ApacheSimpleGetRequestExecutor extends SimpleGetRequestExecutor<CloseableHttpClient, HttpHost> {
    public ApacheSimpleGetRequestExecutor(RequestHttp requestHttp) {
        super(requestHttp);
    }

    @Override
    public String execute(String uri, String queryParam) throws IOException {
        if (queryParam != null) {
            if (uri.indexOf('?') == -1) {
                uri += '?';
            }
            uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
        }
        HttpGet httpGet = new HttpGet(uri);

        HttpHost httpProxy = requestHttp.getRequestHttpProxy();
        if (httpProxy != null) {
            httpGet.setAuthority(new URIAuthority(httpProxy));
        }

        try {
            return requestHttp.getRequestHttpClient().execute(httpGet, Utf8ResponseHandler.INSTANCE);
        } finally {
            httpGet.reset();
        }
    }
}
